package com.party.business.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.party.business.domain.TComment;
import com.party.business.domain.TPolicy;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.domain.entity.SysUser;
import com.party.system.domain.SysLogininfor;
import com.party.system.service.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zhaoyongke
 * @create: 2024-10-25 14:51
 * @description: 统计
 */

@RestController
@RequestMapping("/business/total")
public class TTotalController extends BaseController {

    @Autowired
    private ISysLogininforService logininforService;


    @GetMapping("/getToTalNum")
    public AjaxResult getToTalNum() {
        // 获取本月的开始时间 和结束时间
        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", getBeginTime());
        params.put("endTime", getEndTime());

        SysLogininfor sysLogininfor = new SysLogininfor();
        sysLogininfor.setParams(params);
        // 获取登录信息列表
        List<SysLogininfor> logininforList = logininforService.selectLogininforList(sysLogininfor);
        // logininforList  stream 获取当天的数据
        List<SysLogininfor> todayList = logininforList.stream().filter(logininfor -> {
            // 获取登录时间 Mon Mar 10 08:16:12 CST 2025
            Date loginTime = logininfor.getLoginTime();
            // 获取当前时间
            Date nowTime = new Date();
            boolean sameDay = DateUtil.isSameDay(loginTime, nowTime);
            // 判断是否是当天
            return sameDay;
        }).collect(Collectors.toList());
        // 过滤todayList username去重复
        List<String> monethcollect = logininforList.stream().map(SysLogininfor::getUserName).distinct().collect(Collectors.toList());
        List<String> todayListusernameList = todayList.stream().map(SysLogininfor::getUserName).distinct().collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("totalLogins", monethcollect.size());
        map.put("todayLogins", todayListusernameList.size());
        return AjaxResult.success(map);
    }

    private String getBeginTime() {
        // 获取本月的第一天的开始时间
        return LocalDate.now().withDayOfMonth(1).atStartOfDay().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String getEndTime() {
        // 获取本月的最后一天的结束时间
        return LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).atTime(LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }





}
